﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 9/10/2009
 * Time: 1:28 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Buaya.Model;

namespace Buaya.Gui
{
	/// <summary>
	/// Description of ProcessPayrollForm.
	/// </summary>
	public partial class ProcessPayrollForm : BaseForm
	{
		IList<Employee> employees = SessionFacade.FindAllEmployees();
		IList<PayrollCode> payrollCodes = SessionFacade.FindAllPayrollCodes();
		
		public ProcessPayrollForm() : this(new List<Payslip>())
		{
		}
		
		public ProcessPayrollForm(IList<Payslip> p)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			Model = p;
			listView1.Resize += delegate { columnHeader1.Width = listView1.Width - 22 - (columnHeader2.Width + columnHeader3.Width); };
			Util.SwfUtility.SetupComboBox(comboBoxPayrollCode, payrollCodes, "Id", "Code");
			Util.SwfUtility.SetupComboBox(comboBoxEmployees, employees, "Id", "Name");
		}
		
		void ButtonProcessPayrollClick(object sender, EventArgs e)
		{
			Model = checkBoxSelectAll.Checked ? CreatePayslipsFor(employees) : CreatePayslipsFor(employees[comboBoxEmployees.SelectedIndex]);
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			listView1.Items.Clear();
			foreach (Payslip p in Model as IList<Payslip>) {
				ListViewItem li = listView1.Items.Add(p.Employee.Name.ToString());
				li.SubItems.Add(p.GrossPay.ToString("0.00"));
				li.SubItems.Add(p.NetPay.ToString("0.00"));
			}
		}
		
		IList<Payslip> CreatePayslipsFor(Employee employee)
		{
			IList<Employee> employees = new List<Employee>();
			employees.Add(employee);
			return CreatePayslipsFor(employees);
		}
		
		IList<Payslip> CreatePayslipsFor(IList<Employee> employees)
		{
			IList<Payslip> payslips = new List<Payslip>();
			PayrollCode payrollCode = payrollCodes[comboBoxPayrollCode.SelectedIndex];
			foreach (Employee e in employees) {
				Payslip p = new Payslip();
				p.Employee = e;
				
				p.Deductions = 0;
				foreach (Deduction d in SessionFacade.FindDeductionsByEmployeeAndPayrollCode(e, payrollCode)) {
					p.Deductions += d.Amount;
				}
				p.NetPay = p.GrossPay - p.Deductions;
				payslips.Add(p);
			}
			return payslips;
		}
		
		void CheckBoxSelectAllCheckedChanged(object sender, EventArgs e)
		{
			comboBoxEmployees.Enabled = !checkBoxSelectAll.Checked;
		}
		
		void ButtonSaveAndCloseClick(object sender, EventArgs e)
		{
			Save();
			Close();
		}
		
		void Save()
		{
			foreach (Payslip p in Model as IList<Payslip>) {
				SessionFacade.SaveOrUpdatePayslip(p);
			}
		}
		
		void ButtonSaveAndClearClick(object sender, EventArgs e)
		{
			Save();
			ButtonClearClick(sender, e);
		}
		
		void ButtonClearClick(object sender, EventArgs e)
		{
			Model = new List<Payslip>();
		}
	}
}
